use "$working/full_ProdR_new_kl_L1.dta", clear
collapse beta_k_hat_r beta_l_hat_r beta_lk_hat_r beta_k_hat_r_se beta_l_hat_r_se beta_lk_hat_r_se rts rts_se rho_hat rho_hat_se ///
		beta_lag_ln_IND beta_lag_ln_IND_se beta_lag_MA beta_lag_MA_se perc_test se_perc_test (sum) Observations
gen lag = 1
save "$working/temp/tempL1", replace


use "$working/full_ProdR_new_kl_L2.dta", clear
collapse beta_k_hat_r beta_l_hat_r beta_lk_hat_r beta_k_hat_r_se beta_l_hat_r_se beta_lk_hat_r_se rts rts_se rho_hat rho_hat_se ///
		rho_hat2 rho_hat2_se beta_lag_ln_IND beta_lag_ln_IND_se beta_lag_MA beta_lag_MA_se ///
		beta_lag2_ln_IND beta_lag2_ln_IND_se beta_lag2_MA beta_lag2_MA_se ///
		perc_test se_perc_test (sum) Observations
gen lag = 2
save "$working/temp/tempL2", replace

use "$working/full_ProdR_new_kl_L3.dta", clear
collapse beta_k_hat_r beta_l_hat_r beta_lk_hat_r beta_k_hat_r_se beta_l_hat_r_se beta_lk_hat_r_se rts rts_se rho_hat rho_hat_se ///
		rho_hat2 rho_hat2_se rho_hat3 rho_hat3_se beta_lag_ln_IND beta_lag_ln_IND_se beta_lag_MA beta_lag_MA_se ///
		beta_lag2_ln_IND beta_lag2_ln_IND_se beta_lag2_MA beta_lag2_MA_se beta_lag3_ln_IND beta_lag3_ln_IND_se beta_lag3_MA beta_lag3_MA_se ///
		perc_test se_perc_test (sum) Observations
gen lag = 3

append using "$working/temp/tempL2"
append using "$working/temp/tempL1"
sort lag

xpose, v clear
gen ord = 1 if _varname == "beta_k_hat_r"
replace ord = 1.5 if _varname == "beta_k_hat_r_se"
replace _varname = "$\widetilde{\beta}_K$" if _varname == "beta_k_hat_r" 
replace _varname = "" if _varname == "beta_k_hat_r_se"
replace ord = 2 if _varname == "beta_l_hat_r"
replace ord = 2.5 if _varname == "beta_l_hat_r_se"
replace _varname =  "$\widetilde{\beta}_L$" if _varname == "beta_l_hat_r"
replace _varname = "" if _varname == "beta_l_hat_r_se"
replace ord = 3 if _varname == "beta_lk_hat_r"  
replace ord = 3.5 if _varname == "beta_lk_hat_r_se"  
replace _varname =  "$\widetilde{\beta}_{LK}$" if _varname == "beta_lk_hat_r"  
replace _varname = "" if _varname == "beta_lk_hat_r_se"  
replace ord = 4 if _varname == "rts" 
replace ord = 4.5 if _varname == "rts_se" 
replace _varname =  "$\delta$" if _varname == "rts" 
replace _varname = "" if _varname == "rts_se" 

replace ord = 5 if _varname == "rho_hat"
replace ord = 5.5 if _varname == "rho_hat_se"
replace _varname =  "$\rho^1_H$" if _varname == "rho_hat" 
replace _varname = ""  if _varname == "rho_hat_se"
replace ord = 6 if _varname == "rho_hat2"
replace ord = 6.5 if _varname == "rho_hat2_se"
replace _varname =  "$\rho^2_H$" if _varname == "rho_hat2" 
replace _varname = ""  if _varname == "rho_hat2_se"
replace ord = 7 if _varname == "rho_hat3"
replace ord = 7.5 if _varname == "rho_hat3_se"
replace _varname =  "$\rho^3_H$" if _varname == "rho_hat3" 
replace _varname = ""  if _varname == "rho_hat3_se"

replace ord = 8 if  _varname == "beta_lag_ln_IND"	
replace ord = 8.5 if  _varname == "beta_lag_ln_IND_se"	
replace _varname =  "$\ln\left(OutputComp^{CH}_{n(i),t-1}\right)$" if _varname == "beta_lag_ln_IND" 
replace _varname = "" if  _varname == "beta_lag_ln_IND_se"	
replace ord = 9 if  _varname == "beta_lag2_ln_IND"	
replace ord = 9.5 if  _varname == "beta_lag2_ln_IND_se"	
replace _varname =  "$\ln\left(OutputComp^{CH}_{n(i),t-2}\right)$" if _varname == "beta_lag2_ln_IND" 
replace _varname = "" if  _varname == "beta_lag2_ln_IND_se"	
replace ord = 10 if  _varname == "beta_lag3_ln_IND"	
replace ord = 10.5 if  _varname == "beta_lag3_ln_IND_se"	
replace _varname =  "$\ln\left(OutputComp^{CH}_{n(i),t-3}\right)$" if _varname == "beta_lag3_ln_IND" 
replace _varname = "" if  _varname == "beta_lag3_ln_IND_se"	
replace ord = 11 if  _varname == "beta_lag_MA"	
replace ord = 11.5 if  _varname == "beta_lag_MA_se"	
replace _varname =  "$\ln\left(InputAccess^{CH}_{n(i),t-1}\right)$" if _varname == "beta_lag_MA" 
replace _varname = "" if  _varname == "beta_lag_MA_se"	
replace ord = 12 if  _varname == "beta_lag2_MA"	
replace ord = 12.5 if  _varname == "beta_lag2_MA_se"	
replace _varname =  "$\ln\left(InputAccess^{CH}_{n(i),t-2}\right)$" if _varname == "beta_lag2_MA" 
replace _varname = "" if  _varname == "beta_lag2_MA_se"	
replace ord = 13 if  _varname == "beta_lag3_MA"	
replace ord = 13.5 if  _varname == "beta_lag3_MA_se"	
replace _varname =  "$\ln\left(InputAccess^{CH}_{n(i),t-3}\right)$" if _varname == "beta_lag3_MA" 
replace _varname = "" if  _varname == "beta_lag3_MA_se"	

replace ord = 14 if _varname == "perc_test"
replace ord = 14.5 if _varname == "se_perc_test"
replace _varname =  "Autocorrelation Test" if _varname == "perc_test" 
replace _varname = ""  if _varname == "se_perc_test"
replace ord = 15 if _varname == "Observations"
drop if missing(ord)

sort ord
forval i=1/3 {
	tostring v`i', force replace format(%9.3f)
	replace v`i' = "(" + v`i' + ")" if inlist(ord, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5)
	replace v`i' = "" if v`i' == "." | v`i' == "(.)"
}
la var _varname "Parameter"
texsave _varname v1 v2 v3 using "$tables/production_parameter_new_kl.tex", ///
	footnote("Notes: Reports parameters from revenue production function based on GMM estimation of (\ref{eq: rprod2}); see text for further details. Autocorrelation Test reports coefficient obtained by regressing residuals on lagged residuals. Standard errors, clustered by firm, in parentheses.") ///
	title("Revenue production function paramters") ///
	nofix  replace location(h)  varlab marker(tab:production_paramters_new_kl) frag ///
	autonumber hlines(26) width(.8\textwidth)

**************************************************************************************************************
************************************************* Gross Output ***********************************************
**************************************************************************************************************
use "$working/full_ProdQ_new_klme_L1.dta", clear
collapse beta_k_hat_q beta_l_hat_q beta_m_hat_q beta_e_hat_q beta_lk_hat_q beta_km_hat_q beta_lm_hat_q beta_le_hat_q beta_ke_hat_q beta_me_hat_q ///
	 beta_k_hat_q_se beta_l_hat_q_se beta_m_hat_q_se beta_e_hat_q_se beta_lk_hat_q_se beta_km_hat_q_se beta_lm_hat_q_se beta_le_hat_q_se beta_ke_hat_q_se beta_me_hat_q_se ///
	 rho_hat rho_hat_se beta_lag_ln_IND beta_lag_ln_IND_se  beta_lag_MA beta_lag_MA_se ///
	 perc_test se_perc_test (sum) Observations
gen lag = 1
save "$working/temp/tempL1", replace


use "$working/full_ProdQ_new_klme_L2.dta", clear
collapse beta_k_hat_q beta_l_hat_q beta_m_hat_q beta_e_hat_q beta_lk_hat_q beta_km_hat_q beta_lm_hat_q beta_le_hat_q beta_ke_hat_q beta_me_hat_q ///
	 beta_k_hat_q_se beta_l_hat_q_se beta_m_hat_q_se beta_e_hat_q_se beta_lk_hat_q_se beta_km_hat_q_se beta_lm_hat_q_se beta_le_hat_q_se beta_ke_hat_q_se beta_me_hat_q_se ///
	 rho_hat rho_hat_se beta_lag_ln_IND beta_lag_ln_IND_se  beta_lag_MA beta_lag_MA_se ///
	 rho_hat2 rho_hat2_se beta_lag2_ln_IND beta_lag2_ln_IND_se  beta_lag2_MA beta_lag2_MA_se ///
	 perc_test se_perc_test (sum) Observations
gen lag = 2


append using "$working/temp/tempL1"
sort lag

xpose, v clear
gen ord = 1 if _varname == "beta_k_hat_q"
replace ord = 1.5 if _varname == "beta_k_hat_q_se"
replace _varname = "$\beta_K$" if _varname == "beta_k_hat_q" 
replace _varname = "" if _varname == "beta_k_hat_q_se" 
replace ord = 2 if _varname == "beta_l_hat_q"
replace ord = 2.5 if _varname == "beta_l_hat_q_se"
replace _varname = "$\beta_L$" if _varname == "beta_l_hat_q"
replace _varname = "" if _varname == "beta_l_hat_q_se"
replace ord = 3 if _varname == "beta_m_hat_q"
replace ord = 3.5 if _varname == "beta_m_hat_q_se"
replace _varname = "$\beta_M$" if _varname == "beta_m_hat_q"
replace _varname = "" if _varname == "beta_m_hat_q_se"
replace ord = 4 if _varname == "beta_e_hat_q"
replace ord = 4.5 if _varname == "beta_e_hat_q_se"
replace _varname = "$\beta_E$" if _varname == "beta_e_hat_q"
replace _varname = "" if _varname == "beta_e_hat_q_se"
replace ord = 5 if _varname == "beta_lk_hat_q"
replace ord = 5.5 if _varname == "beta_lk_hat_q_se"
replace _varname = "$\beta_{LK}$" if _varname == "beta_lk_hat_q"
replace _varname = "" if _varname == "beta_lk_hat_q_se"
replace ord = 6 if _varname == "beta_km_hat_q"
replace ord = 6.5 if _varname == "beta_km_hat_q_se"
replace _varname = "$\beta_{KM}$" if _varname == "beta_km_hat_q"
replace _varname = "" if _varname == "beta_km_hat_q_se"
replace ord = 7 if _varname == "beta_lm_hat_q"
replace ord = 7.5 if _varname == "beta_lm_hat_q_se"
replace _varname = "$\beta_{LM}$" if _varname == "beta_lm_hat_q"
replace _varname = "" if _varname == "beta_lm_hat_q_se"
replace ord = 8 if _varname == "beta_le_hat_q"
replace ord = 8.5 if _varname == "beta_le_hat_q_se"
replace _varname = "$\beta_{LE}$" if _varname == "beta_le_hat_q"
replace _varname = "" if _varname == "beta_le_hat_q_se"
replace ord = 9 if _varname == "beta_ke_hat_q"
replace ord = 9.5 if _varname == "beta_ke_hat_q_se"
replace _varname = "$\beta_{KE}$" if _varname == "beta_ke_hat_q"
replace _varname = "" if _varname == "beta_ke_hat_q_se"
replace ord = 10 if _varname == "beta_me_hat_q"
replace ord = 10.5 if _varname == "beta_me_hat_q_se"
replace _varname = "$\beta_{ME}$" if _varname == "beta_me_hat_q"
replace _varname = "" if _varname == "beta_me_hat_q_se"

replace ord = 11 if _varname == "rho_hat"
replace ord = 11.5 if _varname == "rho_hat_se"
replace _varname =  "$\rho^1_H$" if _varname == "rho_hat" 
replace _varname = ""  if _varname == "rho_hat_se"
replace ord = 12 if _varname == "rho_hat2"
replace ord = 12.5 if _varname == "rho_hat2_se"
replace _varname =  "$\rho^2_H$" if _varname == "rho_hat2" 
replace _varname = ""  if _varname == "rho_hat2_se"
replace ord = 13 if  _varname == "beta_lag_ln_IND"	
replace ord = 13.5 if  _varname == "beta_lag_ln_IND_se"	
replace _varname =  "$\ln\left(OutputComp^{CH}_{n(i),t-1}\right)$" if _varname == "beta_lag_ln_IND" 
replace _varname = "" if  _varname == "beta_lag_ln_IND_se"	
replace ord = 14 if  _varname == "beta_lag2_ln_IND"	
replace ord = 14.5 if  _varname == "beta_lag2_ln_IND_se"	
replace _varname =  "$\ln\left(OutputComp^{CH}_{n(i),t-2}\right)$" if _varname == "beta_lag2_ln_IND" 
replace _varname = "" if  _varname == "beta_lag2_ln_IND_se"	
replace ord = 15 if  _varname == "beta_lag_MA"	
replace ord = 15.5 if  _varname == "beta_lag_MA_se"	
replace _varname =  "$\ln\left(InputAccess^{CH}_{n(i),t-1}\right)$" if _varname == "beta_lag_MA" 
replace _varname = "" if  _varname == "beta_lag_MA_se"
replace ord = 16 if  _varname == "beta_lag2_MA"	
replace ord = 16.5 if  _varname == "beta_lag2_MA_se"	
replace _varname =  "$\ln\left(InputAccess^{CH}_{n(i),t-2}\right)$" if _varname == "beta_lag2_MA" 
replace _varname = "" if  _varname == "beta_lag2_MA_se"

replace ord = 17 if _varname == "perc_test"
replace ord = 17.5 if _varname == "se_perc_test"
replace _varname =  "Autocorrelation Test" if _varname == "perc_test" 
replace _varname = ""  if _varname == "se_perc_test"
replace ord = 18 if _varname == "Observations"
drop if missing(ord)

sort ord
forval i=1/2 {
	tostring v`i', force replace format(%9.3f)
	replace v`i' = "(" + v`i' + ")" if inlist(ord, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5, 16.5, 17.5)
	replace v`i' = "" if v`i' == "." | v`i' == "(.)"
}


la var _varname "Parameter"
texsave _varname v1 v2 using "$tables/production_parameter_new_klme.tex", ///
	footnote("Notes: Reports parameters from quantity production function based on GMM estimation of (\ref{eq: prod_fun_t2}); see text for further details. Autocorrelation Test reports coefficient obtained by regressing residuals on lagged residuals. Standard errors, clustered by firm, in parentheses.") ///
	title("Quantity production function parameters") ///
	nofix  replace location(h)  varlab marker(tab:production_paramters_new_klme) frag ///
	autonumber hlines(32) width(.8\textwidth)

